ema <- function(signal, period){
	#Inicializar el vector todos los elementos en cero
	ema = 0 * signal;
	
	#Armo la constante
	k <- 2 / ( period + 1);
	
	#Genero el EMA
	for(i in 2:length(signal)){
		ema[i] = signal[i] * k + ema[i-1] * (1-k)
	}
	
	ema
}

tsData <- read.csv("table.csv");
closePrices <- tsData$Close;

ema34 <- ema(closePrices, 34);
ema17 <- ema(closePrices, 17);

MACD <- ema17 - ema34;

Signal <- ema(MACD, 9);

Histogram <- MACD - Signal;
#x<- 0:500;
#y<- -40:120;
#plot(closePrices, type="l", col="black", ylim=c(-40,120));
#lines(MACD, col="red");
#lines(Signal, col="blue");
#lines(Histogram, col="yellow");
